package model;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Vector;

import javax.microedition.lcdui.Image;

import main.NuoiThuMidlet;
import service.GlobalService;
import utilities.CRes;


public class DataManager {
	//links save items in RMS
	private static final String m_strLinkImg = "BigImg";
    private static final String m_strLinkItemType = "ItemType";
    
    private static final String m_strLinkListToyInfo = "ListToyInfo";
    private static final String m_strLinkListAnimalInfo = "ListAnimalInfo";
    private static final String m_strLinkListAvatarInfo = "ListAvatarInfo";
    private static final String m_strLinkListFoodInfo = "ListFoodInfo";
    private static final String m_strLinkListDrugInfo = "ListDrugInfo";
    
    //list of items's info in shop
    private Vector m_listAnimalInfo = null;
    private Vector m_listToyInfo = null;
    private Vector m_listAvatarInfo = null;
    private Vector m_listFoodInfo = null;
    private Vector m_listDrugInfo = null;
    
    //list of my items in stock
    public Vector m_myLstAnimal = new Vector();
    public Vector m_myLstToy = new Vector();
    public Vector m_myLstAvatar = new Vector();
    public Vector m_myLstFood = new Vector();
    public Vector m_myLstDrug = new Vector();
    
    //cached data
    public Vector m_myListFriend = null;
    public Vector m_vtSMS = null;
    public Vector m_vtSearchFriend = null;
    public Vector m_vtDiary = null;

    //cached image items
	public ImageData[] m_arrImg;
    
	//stuffs
	public String m_strForumLink = null;
	
    private static DataManager instance = new DataManager();

    public static DataManager gI() {
        return instance;
    }
    
    private static int m_iCountRequest = 0;
    
  //init all description for items in shop
    public void initAnimalInfoDes(){
    	if (m_listAnimalInfo != null) {
            int a = m_listAnimalInfo.size();
            for (int i = 0; i < a; i++) {
                AnimalInfo ani = (AnimalInfo) m_listAnimalInfo.elementAt(i);
                ani.initDescription();
            }
        }
    }
    public void initToyInfoDes(){
    	if (m_listToyInfo != null) {
            int a = m_listToyInfo.size();
            for (int i = 0; i < a; i++) {
                ToyInfo toy = (ToyInfo) m_listToyInfo.elementAt(i);
                toy.initDescription();
            }
        }
    }
    public void initAvatarInfoDes(){
    	if (m_listAvatarInfo != null) {
            int a = m_listAvatarInfo.size();
            for (int i = 0; i < a; i++) {
                AvatarInfo A = (AvatarInfo) m_listAvatarInfo.elementAt(i);
                A.initDescription();
            }
        }
    }
    public void initFoodInfoDes(){
    	if (m_listFoodInfo != null) {
            int a = m_listFoodInfo.size();
            for (int i = 0; i < a; i++) {
                FoodInfo A = (FoodInfo) m_listFoodInfo.elementAt(i);
                A.initDescription();
            }
        }
    }
    public void initDrugInfoDes(){
    	if (m_listDrugInfo != null) {
            int a = m_listDrugInfo.size();
            for (int i = 0; i < a; i++) {
                DrugInfo A = (DrugInfo) m_listDrugInfo.elementAt(i);
                A.initDescription();
            }
        }
    }
    
    public Vector getListAnimalInfo() {
        return m_listAnimalInfo;
    }

    public Vector getListToyInfo() {
        return m_listToyInfo;
    }

    public Vector getListAvatarInfo() {
        return m_listAvatarInfo;
    }

    public Vector getListFoodInfo() {
        return m_listFoodInfo;
    }

    public Vector getListDrugInfo() {
        return m_listDrugInfo;
    }

    public int getMyNumItem(int itemType, int itemId){
    	int i, count;
    	switch(itemType){
    	case Item.ITEM_TYPE_ANIMAL:
    		
    		break;
    	case Item.ITEM_TYPE_AVATAR:
    		break;
    	case Item.ITEM_TYPE_DRUG:
    		count = m_myLstDrug.size();
    		for(i = 0; i < count; i++){
    			Item item = (Item)m_myLstDrug.elementAt(i);
    			if(item.m_iId == itemId)
    				return item.m_iNumItem;
    		}
    		break;
    	case Item.ITEM_TYPE_FOOD:
    		count = m_myLstFood.size();
    		for(i = 0; i < count; i++){
    			Item item = (Item)m_myLstFood.elementAt(i);
    			if(item.m_iId == itemId)
    				return item.m_iNumItem;
    		}
    		break;
    	case Item.ITEM_TYPE_TOY:
    		break;
    	}
    	return 0;
    }
    
    public ToyInfo getToyInfo(int id) {
        if (m_listToyInfo != null) {
            int a = m_listToyInfo.size();
            for (int i = 0; i < a; i++) {
                ToyInfo info = (ToyInfo) m_listToyInfo.elementAt(i);
                if (info.m_iId == id) {
                    return info;
                }
            }
        }
        return null;
    }

    public AnimalInfo getAnimalInfo(int id) {
        if (m_listAnimalInfo != null) {
            int a = m_listAnimalInfo.size();
            for (int i = 0; i < a; i++) {
                AnimalInfo info = (AnimalInfo) m_listAnimalInfo.elementAt(i);
                if (info.m_iId == id) {
                    return info;
                }
            }
        }
        return null;
    }

    public AvatarInfo getAvatarInfo(int id) {
        if (m_listAvatarInfo != null) {
            int a = m_listAvatarInfo.size();
            for (int i = 0; i < a; i++) {
                AvatarInfo info = (AvatarInfo) m_listAvatarInfo.elementAt(i);
                if (info.m_iId == id) {
                    return info;
                }
            }
        }
        return null;
    }
    public FoodInfo getFoodInfo(int id) {
        if (m_listFoodInfo != null) {
            int a = m_listFoodInfo.size();
            for (int i = 0; i < a; i++) {
            	FoodInfo info = (FoodInfo) m_listFoodInfo.elementAt(i);
                if (info.m_iId == id) {
                    return info;
                }
            }
        }
        return null;
    }
    public DrugInfo getDrugInfo(int id) {
        if (m_listDrugInfo != null) {
            int a = m_listDrugInfo.size();
            for (int i = 0; i < a; i++) {
            	DrugInfo info = (DrugInfo) m_listDrugInfo.elementAt(i);
                if (info.m_iId == id) {
                    return info;
                }
            }
        }
        return null;
    }
    
    public Item getMyItem(int itemType, int itemID){
    	int i, count;
    	
    	switch(itemType){
    	case Item.ITEM_TYPE_AVATAR:
    		count = m_myLstAvatar.size();
    		for(i = 0; i < count; i++)
    		{
    			Item f = (Item)m_myLstAvatar.elementAt(i);
    			if(f.m_iId == itemID)
    				return f;
    		}
    		break;
    	case Item.ITEM_TYPE_FOOD:
    		count = m_myLstFood.size();
    		for(i = 0; i < count; i++)
    		{
    			Item f = (Item)m_myLstFood.elementAt(i);
    			if(f.m_iId == itemID)
    				return f;
    		}
    		break;
    	case Item.ITEM_TYPE_DRUG:
    		count = m_myLstDrug.size();
    		for(i = 0; i < count; i++)
    		{
    			Item f = (Item)m_myLstDrug.elementAt(i);
    			if(f.m_iId == itemID)
    				return f;
    		}    		
    		break;
    	case Item.ITEM_TYPE_TOY:
    		count = m_myLstToy.size();
    		for(i = 0; i < count; i++)
    		{
    			Item f = (Item)m_myLstToy.elementAt(i);
    			if(f.m_iId == itemID)
    				return f;
    		}    		
    		break;
    	case Item.ITEM_TYPE_ANIMAL:
    		break;
    	}
    	return null;
    }
    
  //check cached data is existed
    public void checkGardenData(Vector listItemType) {
        // TODO Auto-generated method stub
        m_iCountRequest = 0;
        int a = listItemType.size();
        for (int i = 0; i < a; i++) {
            //server
        	ItemInfo itemInfo = (ItemInfo) listItemType.elementAt(i);
            
            //load rms
            ItemInfo loadItemInfo = loadItemInfo(itemInfo.m_bItemType);
            
            if ((loadItemInfo == null) || (loadItemInfo.m_iVersion != itemInfo.m_iVersion)) {
                //request item info when it is not exist or the data out of date
                GlobalService.gI().getListItem(itemInfo.m_bItemType);
                m_iCountRequest++;
                
                saveItemInfo(itemInfo);
                loadItemInfo = null;
            } else {
                //load data from RMS
                switch (itemInfo.m_bItemType) {
                    case Item.ITEM_TYPE_ANIMAL:
                        loadListAnimalInfo();
                        break;
                    case Item.ITEM_TYPE_TOY:
                        loadListToyInfo();
                        break;
                    case Item.ITEM_TYPE_AVATAR:
                        loadListAvatarInfo();
                        break;
                    case Item.ITEM_TYPE_DRUG:
                    	loadListDrugInfo();
                    	break;
                    case Item.ITEM_TYPE_FOOD:
                    	loadListFoodInfo();
                    	break;
                     
                }
            }
        }

        CRes.cleanVector(listItemType);
        requestMyGardenData();
    }

    private void loadListFoodInfo() {
		// TODO Auto-generated method stub
    	if (m_listFoodInfo == null) {
            byte[] bData = CRes.loadRMS(m_strLinkListFoodInfo);
            if (bData == null) {
                return;
            }

            ByteArrayInputStream bi = new ByteArrayInputStream(bData);
            DataInputStream dis = new DataInputStream(bi);
            try {
                m_listFoodInfo = new Vector();

                int count = dis.readShort();
                for (int i = 0; i < count; i++) {
                	FoodInfo f = new FoodInfo();

                    f.m_bItemType = dis.readByte();
                    f.m_iId = dis.readByte();
                    f.m_sImgID = dis.readShort();
                    f.m_strName = dis.readUTF();
                    f.m_sType = dis.readShort();
                    f.m_iMoney[0] = dis.readInt();
                    f.m_iMoney[1] = dis.readInt();
                    f.m_sExperience = dis.readShort();
                    f.m_sLevelRequired = dis.readShort();
                    f.m_sHealth = dis.readShort();
                    f.importFrameImg(dis.readUTF());
                   
                    m_listFoodInfo.addElement(f);
                }
                
                initFoodInfoDes();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
	}
    
    public void saveListFoodInfo(Vector v) {
        // TODO Auto-generated method stub
        CRes.cleanVector(m_listFoodInfo);
        m_listFoodInfo = v;
        initFoodInfoDes();
        
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bo);
        try {
            int a = v.size();
            dos.writeShort(a);
            for (int i = 0; i < a; i++) {
                FoodInfo t = (FoodInfo) v.elementAt(i);

                dos.writeByte(t.m_bItemType);
                dos.writeByte(t.m_iId);
                dos.writeShort(t.m_sImgID);
                dos.writeUTF(t.m_strName);
                dos.writeShort(t.m_sType);
                dos.writeInt(t.m_iMoney[0]);
                dos.writeInt(t.m_iMoney[1]);
                dos.writeShort(t.m_sExperience);
                dos.writeShort(t.m_sLevelRequired);
                dos.writeShort(t.m_sHealth);
                dos.writeUTF(t.m_strFrameImg);
            }

            byte[] bData = bo.toByteArray();
            CRes.saveRMS(m_strLinkListFoodInfo, bData);
            dos.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        m_iCountRequest--;
        requestMyGardenData();
    }
    

	private void loadListDrugInfo() {
	// TODO Auto-generated method stub
		if (m_listDrugInfo == null) {
            byte[] bData = CRes.loadRMS(m_strLinkListDrugInfo);
            if (bData == null) {
                return;
            }

            ByteArrayInputStream bi = new ByteArrayInputStream(bData);
            DataInputStream dis = new DataInputStream(bi);
            try {
            	m_listDrugInfo = new Vector();

                int count = dis.readShort();
                for (int i = 0; i < count; i++) {
                	DrugInfo f = new DrugInfo();

                    f.m_bItemType = dis.readByte();
                    f.m_iId = dis.readByte();
                    f.m_sImgID = dis.readShort();
                    f.m_strName = dis.readUTF();
                    f.m_iMoney[0] = dis.readInt();
                    f.m_iMoney[1] = dis.readInt();
                    f.m_sExperience = dis.readShort();
                    f.m_sLevelRequired = dis.readShort();
                    f.m_strDes = dis.readUTF();
                    f.importFrameImg(dis.readUTF());
                    
                    m_listDrugInfo.addElement(f);
                }
                
                initDrugInfoDes();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
	}
	
	public void saveListDrugInfo(Vector v) {
        // TODO Auto-generated method stub
        CRes.cleanVector(m_listDrugInfo);
        m_listDrugInfo = v;
        initDrugInfoDes();

        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bo);
        try {
            int a = v.size();
            dos.writeShort(a);
            for (int i = 0; i < a; i++) {
                DrugInfo t = (DrugInfo) v.elementAt(i);

                dos.writeByte(t.m_bItemType);
                dos.writeByte(t.m_iId);
                dos.writeShort(t.m_sImgID);
                dos.writeUTF(t.m_strName);
                dos.writeInt(t.m_iMoney[0]);
                dos.writeInt(t.m_iMoney[1]);
                dos.writeShort(t.m_sExperience);
                dos.writeShort(t.m_sLevelRequired);
                dos.writeUTF(t.m_strDes);
                dos.writeUTF(t.m_strFrameImg);
            }

            byte[] bData = bo.toByteArray();
            CRes.saveRMS(m_strLinkListDrugInfo, bData);
            dos.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        m_iCountRequest--;
        requestMyGardenData();
    }

	//load item type form RMS
    private ItemInfo loadItemInfo(byte itemType) {
        byte[] bData = CRes.loadRMS(m_strLinkItemType + itemType);
        if (bData == null) {
            return null;
        }
        ByteArrayInputStream bi = new ByteArrayInputStream(bData);
        DataInputStream dis = new DataInputStream(bi);
        try {
            ItemInfo itemInfo = new ItemInfo();
            itemInfo.m_bItemType = dis.readByte();
            itemInfo.m_strName = dis.readUTF();
            itemInfo.m_iVersion = dis.readInt();

            return itemInfo;
        } catch (Exception ex) {
            return null;
        }
    }

    //save item info to RMS
    private void saveItemInfo(ItemInfo itemInfo) {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bo);
        try {

            dos.write(itemInfo.m_bItemType);
            dos.writeUTF(itemInfo.m_strName);
            dos.writeInt(itemInfo.m_iVersion);

            byte[] bData = bo.toByteArray();
            CRes.saveRMS(m_strLinkItemType + itemInfo.m_bItemType, bData);
            dos.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    //request my garden data
    public void requestMyGardenData() {
        if (m_iCountRequest == 0) {
            GlobalService.gI().getGardenData(NuoiThuMidlet.m_myPlayerInfo.m_iId);
        }
    }

    ////////////
    public void loadListToyInfo() {
        if (m_listToyInfo == null) {
            byte[] bData = CRes.loadRMS(m_strLinkListToyInfo);
            if (bData == null) {
                return;
            }

            ByteArrayInputStream bi = new ByteArrayInputStream(bData);
            DataInputStream dis = new DataInputStream(bi);
            try {
                m_listToyInfo = new Vector();

                int count = dis.readShort();
                for (int i = 0; i < count; i++) {
                    ToyInfo f = new ToyInfo();

                    f.m_bItemType = dis.readByte();
                    f.m_iId = dis.readByte();
                    f.m_sImgID = dis.readShort();
                    f.m_strName = dis.readUTF();
                    f.m_iMoney[0] = dis.readInt();
                    f.m_iMoney[1] = dis.readInt();
                    f.m_sExperience = dis.readShort();
                    f.m_sLevelRequired = dis.readShort();
                    f.m_sTimeExpired = dis.readShort();
                    f.importFrameImg(dis.readUTF());
                    f.m_bIsSell = dis.readBoolean();

                    m_listToyInfo.addElement(f);
                }
                
                initToyInfoDes();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public void saveListToyInfo(Vector v) {
        // TODO Auto-generated method stub
        CRes.cleanVector(m_listToyInfo);
        m_listToyInfo = v;
        initToyInfoDes();

        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bo);
        try {
            int a = v.size();
            dos.writeShort(a);
            for (int i = 0; i < a; i++) {
                ToyInfo t = (ToyInfo) v.elementAt(i);

                dos.writeByte(t.m_bItemType);
                dos.writeByte(t.m_iId);
                dos.writeShort(t.m_sImgID);
                dos.writeUTF(t.m_strName);
                dos.writeInt(t.m_iMoney[0]);
                dos.writeInt(t.m_iMoney[1]);
                dos.writeShort(t.m_sExperience);
                dos.writeShort(t.m_sLevelRequired);
                dos.writeShort(t.m_sTimeExpired);
                dos.writeUTF(t.m_strFrameImg);
                dos.writeBoolean(t.m_bIsSell);
            }

            byte[] bData = bo.toByteArray();
            CRes.saveRMS(m_strLinkListToyInfo, bData);
            dos.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        m_iCountRequest--;
        requestMyGardenData();
    }

    public void loadListAnimalInfo() {
        if (m_listAnimalInfo == null) {
            byte[] bData = CRes.loadRMS(m_strLinkListAnimalInfo);
            if (bData == null) {
                return;
            }

            ByteArrayInputStream bi = new ByteArrayInputStream(bData);
            DataInputStream dis = new DataInputStream(bi);
            try {
                m_listAnimalInfo = new Vector();

                int count = dis.readShort();
                for (int i = 0; i < count; i++) {
                    AnimalInfo f = new AnimalInfo();

                    f.m_bItemType = dis.readByte();
                    f.m_iId = dis.readShort();
                    f.m_strName = dis.readUTF();
                    f.m_iMoney[0] = dis.readInt();
                    f.m_iMoney[1] = dis.readInt();
                    f.m_sFood = dis.readShort();                    
                    f.m_sExperience = dis.readShort();
                    f.m_sHealth = dis.readShort();
                    f.m_sTimeLiving = dis.readShort();
                    f.m_sTimeCreateMoney = dis.readShort();
                    f.m_sTimeFeed = dis.readShort();
                    f.m_sLevelRequired = dis.readShort();
                    f.m_sTimeTakeABath = dis.readShort();
                    f.m_sMoneyHarvest = dis.readShort();
                    f.m_sMoneyPeriod = dis.readShort();
                    f.m_strHead = dis.readUTF();
                    f.m_strBody = dis.readUTF();
                    f.m_strSmallHead = dis.readUTF();
                    f.m_strSmallBody = dis.readUTF();
                    f.importProperties(dis.readUTF());
                                        
                    AnimalInfo.importPart(f.m_headPart[1], f.m_strHead);
                    AnimalInfo.importPart(f.m_bodyPart[1], f.m_strBody);
                    AnimalInfo.importSmallPart(f.m_bodyPart[0], f.m_strSmallBody);
                    AnimalInfo.importSmallPart(f.m_headPart[0], f.m_strSmallHead);
                                        
                    m_listAnimalInfo.addElement(f);
                }
                
                initAnimalInfoDes();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public void saveListAnimalInfo(Vector v) {
        // TODO Auto-generated method stub
        CRes.cleanVector(m_listAnimalInfo);
        m_listAnimalInfo = v;
        initAnimalInfoDes();        
        
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bo);
        try {
            int a = v.size();
            dos.writeShort(a);
            for (int i = 0; i < a; i++) {
                AnimalInfo animal = (AnimalInfo) v.elementAt(i);

                dos.writeByte(animal.m_bItemType);
                dos.writeShort(animal.m_iId);
                dos.writeUTF(animal.m_strName);
                dos.writeInt(animal.m_iMoney[0]);
                dos.writeInt(animal.m_iMoney[1]);
                dos.writeShort(animal.m_sFood);
                dos.writeShort(animal.m_sExperience);
                dos.writeShort(animal.m_sHealth);
                dos.writeShort(animal.m_sTimeLiving);
                dos.writeShort(animal.m_sTimeCreateMoney);
                dos.writeShort(animal.m_sTimeFeed);
                dos.writeShort(animal.m_sLevelRequired);
                dos.writeShort(animal.m_sTimeTakeABath);
                dos.writeShort(animal.m_sMoneyHarvest);
                dos.writeShort(animal.m_sMoneyPeriod);
                dos.writeUTF(animal.m_strHead);
                dos.writeUTF(animal.m_strBody);
                dos.writeUTF(animal.m_strSmallHead);
                dos.writeUTF(animal.m_strSmallBody);
                dos.writeUTF(animal.m_strProperties);
                
            }

            byte[] bData = bo.toByteArray();
            CRes.saveRMS(m_strLinkListAnimalInfo, bData);
            dos.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        m_iCountRequest--;
        requestMyGardenData();
    }

    public void loadListAvatarInfo() {
        if (m_listAvatarInfo == null) {
            byte[] bData = CRes.loadRMS(m_strLinkListAvatarInfo);
            if (bData == null) {
                return;
            }
            
            ByteArrayInputStream bi = new ByteArrayInputStream(bData);
            DataInputStream dis = new DataInputStream(bi);
            try {
                m_listAvatarInfo = new Vector();

                int count = dis.readShort();
                for (int i = 0; i < count; i++) {
                    AvatarInfo f = new AvatarInfo();

                    f.m_bItemType = dis.readByte();
                    f.m_iId = dis.readByte();
                    f.m_sImgID = dis.readShort();
                    f.m_strName = dis.readUTF();
                    f.m_iMoney[0] = dis.readInt();
                    f.m_iMoney[1] = dis.readInt();
					f.m_sExperience = dis.readShort();
					f.m_sLevelRequired = dis.readShort();
					f.m_sTimeExpired = dis.readShort();
					f.importFrameImg(dis.readUTF());
					f.m_bIsSell = dis.readBoolean();

                    m_listAvatarInfo.addElement(f);
                }
                
                initAvatarInfoDes();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public void saveListAvatarInfo(Vector v) {
        // TODO Auto-generated method stub
        CRes.cleanVector(m_listAvatarInfo);
        m_listAvatarInfo = v;
        initAvatarInfoDes();
        
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bo);
        try {
            int a = v.size();
            dos.writeShort(a);
            for (int i = 0; i < a; i++) {
                AvatarInfo item = (AvatarInfo) v.elementAt(i);

                dos.writeByte(item.m_bItemType);
                dos.writeByte(item.m_iId);
                dos.writeShort(item.m_sImgID);
                dos.writeUTF(item.m_strName);
                dos.writeInt(item.m_iMoney[0]);
                dos.writeInt(item.m_iMoney[1]);
				dos.writeShort(item.m_sExperience);
				dos.writeShort(item.m_sLevelRequired);
				dos.writeShort(item.m_sTimeExpired);
				dos.writeUTF(item.m_strFrameImg);
				dos.writeBoolean(item.m_bIsSell);
            }

            byte[] bData = bo.toByteArray();
            CRes.saveRMS(m_strLinkListAvatarInfo, bData);
            dos.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        m_iCountRequest--;
        requestMyGardenData();
    }
        
    public void cleanUserCache() {
    	CRes.cleanVector(m_myListFriend);
    	CRes.cleanVector(m_myLstAnimal);
    	CRes.cleanVector(m_myLstAvatar);
    	CRes.cleanVector(m_myLstDrug);
    	CRes.cleanVector(m_myLstFood);
    	CRes.cleanVector(m_myLstToy);
    	CRes.cleanVector(m_vtDiary);
    	CRes.cleanVector(m_vtSearchFriend);
    	CRes.cleanVector(m_vtSMS);
    	
    	m_vtSMS = null;
    	m_vtSearchFriend = null;
    	m_vtDiary = null;
    	m_myListFriend = null;
    	m_strForumLink = null;
    }
    
    public void cleanDataCache() {
        try {
            for (int i = 0; i < 9; i++) {
                try {
                    CRes.deleteRMS(m_strLinkItemType + i);
                    CRes.deleteRMS(m_strLinkImg + i);
                } catch (Exception ex) {
                }
            }
            
            CRes.deleteRMS(m_strLinkListToyInfo);
            CRes.deleteRMS(m_strLinkListAnimalInfo);
            CRes.deleteRMS(m_strLinkListFoodInfo);
            CRes.deleteRMS(m_strLinkListDrugInfo);
            CRes.deleteRMS(m_strLinkListAvatarInfo);
            
        } catch (Exception ex) {
        }
        
        System.gc();
    }

	public void addItemToContainer(byte itemType, short itemID, short numItem) {
		// TODO Auto-generated method stub
		
		boolean flag = false;
        switch (itemType) {
            case Item.ITEM_TYPE_ANIMAL:
                int a7 = m_myLstAnimal.size();
                for (int i = 0; i < a7; i++) {
                    Animal animal = (Animal) m_myLstAnimal.elementAt(i);
                    if (animal.m_iId == itemID) {
                        flag = true;
                        return;
                    }
                }
                if (!flag) {
                    AnimalInfo aniInfo = getAnimalInfo(itemID);
                    if (aniInfo != null) {
                        Animal animal = new Animal(itemID);
                        
                        animal.m_bItemType = Item.ITEM_TYPE_ANIMAL;
                        animal.m_iId = itemID;
                        animal.m_iNumItem = 1;
                        animal.m_strName = aniInfo.m_strName;
                        animal.itemInfo = aniInfo;
                        
                        m_myLstAnimal.addElement(animal);
                    }
                }
                break;
             
            case Item.ITEM_TYPE_FOOD:
            	int count = m_myLstFood.size();
                for (int i = 0; i < count; i++) {
                    Item toy = (Item) m_myLstFood.elementAt(i);
                    if (toy.m_iId == itemID) {
                        toy.m_iNumItem += numItem;
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    FoodInfo toyInfo = getFoodInfo(itemID);
                    if (toyInfo != null) {
                    	Item toy = new Item();

                    	toy.m_bItemType = Item.ITEM_TYPE_FOOD;
                        toy.m_iId = itemID;
                        toy.m_iNumItem = numItem;
                        toy.m_strName = toyInfo.m_strName;
                        toy.itemInfo = toyInfo;

                        m_myLstFood.addElement(toy);
                    }
                }
                break;
            case Item.ITEM_TYPE_DRUG:
            	count = m_myLstDrug.size();
                for (int i = 0; i < count; i++) {
                    Item toy = (Item) m_myLstDrug.elementAt(i);
                    if (toy.m_iId == itemID) {
                        toy.m_iNumItem += numItem;
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    DrugInfo toyInfo = getDrugInfo(itemID);
                    if (toyInfo != null) {
                    	Item toy = new Item();

                    	toy.m_bItemType = Item.ITEM_TYPE_DRUG;
                        toy.m_iId = itemID;
                        toy.m_iNumItem = numItem;
                        
                        if(toy.m_iId <= 3)
                        	toy.m_strName = "Thuốc trị "+toyInfo.m_strName;
                    	else
                    		toy.m_strName = toyInfo.m_strName;
                        
                        toy.m_strName = toyInfo.m_strName;
                        toy.itemInfo = toyInfo;

                        m_myLstDrug.addElement(toy);
                    }
                }
                break;
           
            default:
                break;
        }
        
	}
	
	public void useItem(int itemType, int itemID, int number){
		int i;
		switch(itemType){
		case Item.ITEM_TYPE_ANIMAL:
			break;
		case Item.ITEM_TYPE_DRUG:
			for(i = 0; i < m_myLstDrug.size(); i++){
				Item item = (Item)m_myLstDrug.elementAt(i);
				if(item.m_iId == itemID){
					item.m_iNumItem -= number;
					if(item.m_iNumItem <= 0)
						m_myLstDrug.removeElementAt(i);
					break;
				}
			}
			break;
		case Item.ITEM_TYPE_FOOD:
			for(i = 0; i < m_myLstFood.size(); i++){
				Item item = (Item)m_myLstFood.elementAt(i);
				if(item.m_iId == itemID){
					item.m_iNumItem -= number;
					if(item.m_iNumItem <= 0)
						m_myLstFood.removeElementAt(i);
					break;
				}
			}
			break;
		}
	}

	public void setMyListItem(Vector vItems) {
		// TODO Auto-generated method stub
		//clear old data		
        m_myLstAnimal.removeAllElements();
        m_myLstFood.removeAllElements();
        m_myLstDrug.removeAllElements();

        //add new data
        int a = vItems.size();
        for (int i = 0; i < a; i++) {
            Item item = (Item) vItems.elementAt(i);
            switch (item.m_bItemType) {
                case Item.ITEM_TYPE_ANIMAL:
                    m_myLstAnimal.addElement(item);
                    AnimalInfo animalInfo = getAnimalInfo(item.m_iId);
                    if (animalInfo != null) {
                        item.m_strName = animalInfo.m_strName;
                        item.itemInfo = animalInfo;
                    }

                    break;
                case Item.ITEM_TYPE_FOOD:
                    m_myLstFood.addElement(item);
                    FoodInfo foodInfo = getFoodInfo(item.m_iId);
                    if (foodInfo != null) {
                        item.m_strName = foodInfo.m_strName;
                        item.m_iId = foodInfo.m_iId;
                        item.itemInfo = foodInfo;
                    }
                    break;
                case Item.ITEM_TYPE_DRUG:
                    m_myLstDrug.addElement(item);
                    DrugInfo drugInfo = getDrugInfo(item.m_iId);
                    if (drugInfo != null) {
                    	if(item.m_iId <= 3)
                    		item.m_strName = "Thuốc trị "+drugInfo.m_strName;
                    	else
                    		item.m_strName = drugInfo.m_strName;
                    	
                        item.m_iId = drugInfo.m_iId;
                        item.itemInfo = drugInfo;
                    }
                    break;
                
                default:
                    break;
            }
        }

        CRes.cleanVector(vItems);
	}

	private void requestListItemType(){
		if (m_iCountRequest == 0) {
            GlobalService.gI().getListItemType();
        }
	}
	
	private ImageData loadImgData(short m_sImgId) {
		// TODO Auto-generated method stub
		byte[] bData = CRes.loadRMS(m_strLinkImg + m_sImgId);
        if (bData == null) {
            return null;
        }
        ByteArrayInputStream bi = new ByteArrayInputStream(bData);
        DataInputStream dis = new DataInputStream(bi);
        try {
        	ImageData imgData = new ImageData();
        	imgData.m_sImgId = dis.readShort();
        	imgData.m_sImgVersion = dis.readShort();
        	int length = dis.readInt();
        	imgData.data = new byte[length];
        	dis.read(imgData.data);

            return imgData;
        } catch (Exception ex) {
            return null;
        }
	}
	
	public Image getImage(int imgID){
		if(imgID >= 0 && imgID < m_arrImg.length){
			return m_arrImg[imgID].m_img;
		}
		else{
			return null;
		}
	}
	
	public void saveImgData(ImageData imgData) {
		m_arrImg[imgData.m_sImgId] = imgData;
		imgData.loadImage();
		
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bo);
        try {
            dos.writeShort(imgData.m_sImgId);
            dos.writeShort(imgData.m_sImgVersion);
            dos.writeInt(imgData.data.length);
            dos.write(imgData.data);
            
            byte[] bData = bo.toByteArray();
            CRes.saveRMS(m_strLinkImg + imgData.m_sImgId, bData);
            dos.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        m_iCountRequest--;
        requestListItemType();
    }

	public void checkImageData(Vector vtImg) {
		// TODO Auto-generated method stub
		m_arrImg = new ImageData[vtImg.size()];
		m_iCountRequest = 0;
		int i;
        int a = m_arrImg.length;
        for (i = 0; i < a; i++) {
            //server
        	ImageData imgData = (ImageData)vtImg.elementAt(i);
        	
            //load rms
            ImageData myImgData = loadImgData(imgData.m_sImgId);
            
            if ((myImgData == null) || (myImgData.m_sImgVersion != imgData.m_sImgVersion)) {
                //request big image when it is not exist or the data out of date
                GlobalService.gI().getImage(imgData.m_sImgId);
                
                m_iCountRequest++;
                myImgData = null;
            	imgData = null;
            } else {
                //load data from RMS
            	imgData = null;
            	m_arrImg[i] = myImgData;
                myImgData.loadImage();
            }
        }

        CRes.cleanVector(vtImg);
        
        requestListItemType();
	}
	public void addToyToContainer(Toy toy) {
		// TODO Auto-generated method stub
        m_myLstToy.addElement(toy);
	}
	public void useAvatar(short avatarId) {
		// TODO Auto-generated method stub
		int i, count = m_myLstAvatar.size();
		for(i = 0; i < count; i++){
			Avatar ava = (Avatar)m_myLstAvatar.elementAt(i);
			if(ava.itemInfo.m_iId == avatarId){
				m_myLstAvatar.removeElement(ava);
				count--;
			}
		}
	}
}
